package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.PortBlueRuleDto;
import com.hualude.roadsign.dto.PortRedRuleDto;
import com.hualude.roadsign.model.PortBlueRuleEntity;
import com.hualude.roadsign.model.PortRedRuleEntity;
import com.hualude.roadsign.repository.PortBlueRuleRepository;
import com.hualude.roadsign.repository.PortRedRuleRepository;
import com.hualude.roadsign.service.PortRuleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by xu_jiaHui on 2017/12/9.
 */
@Service
public class PortRuleServiceImpl implements PortRuleService {

    @Resource
    private PortBlueRuleRepository portBlueRuleRepository;

    @Resource
    private PortRedRuleRepository portRedRuleRepository;

    @Override
    public DTResponseDto getPortRedRuleById(DTRequestDto dtRequestDto, Integer alarmId) throws Exception {
        List<PortRedRuleDto> dtos = portRedRuleRepository.getPortRedRule4DT(dtRequestDto, alarmId);
        Integer count = portRedRuleRepository.getPortRedRuleCount4DT(alarmId);
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, dtos);
    }

    @Override
    public DTResponseDto getPortBlueRuleById(DTRequestDto dtRequestDto, Integer alarmId) throws Exception {
        List<PortBlueRuleDto> dtos = portBlueRuleRepository.getPortBlueRule4DT(dtRequestDto, alarmId);
        Integer count = portBlueRuleRepository.getPortBlueRuleCount4DT(alarmId);
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, dtos);
    }

    @Override
    @Transactional
    public String addPortRedRule(List<PortRedRuleDto> dtos) throws Exception {
      /*  int size = dtos.size();
        int count = 0, count1 = 0, count2 = 0, flag = 0;
        String sendmsg = "";
        for (PortRedRuleDto dto : dtos) {
            if (dto.getRedRuleFlag().equals("100"))
                count1++;
            else {
                count2++;
            }
        }

        Integer iooCount = portRedRuleRepository.getRedIooRuleCount(dtos.get(0).getAlarmId());
        Integer zooCount = portRedRuleRepository.getRedZooRuleCount(dtos.get(0).getAlarmId());
        if (iooCount + count1 > 10) {
            flag = 1;
            sendmsg += "100规则超出10个上限  ";
        } else if (zooCount + count2 > 10) {
            flag = 1;
            sendmsg += "200规则超出10个上限  ";
        }
        if (flag == 1) {
            return sendmsg;
        }
        Long GreenTime = new Date().getTime() / 1000;
        String strGreenTime = GreenTime.toString();//TODO 更新表即执行版本更新
        portRedRuleRepository.updateRuleMark(strGreenTime, dtos.get(0).getAlarmId());
        for (PortRedRuleDto dto : dtos) {
            PortRedRuleEntity entity = dto.toBean();
            entity.setRedRuleMark(strGreenTime);
            Integer id = portRedRuleRepository.save(entity).getRedRuleId();
            if (id > 0)
                count++;
        }*/
        return "1";
    }


    @Override
    @Transactional
    public Integer addPortBlueRule(List<PortBlueRuleDto> dtos) throws Exception {
        int size = dtos.size();
        int count = 0;
        for (PortBlueRuleDto dto : dtos) {
            PortBlueRuleEntity entity = dto.toBean();
            Integer id = portBlueRuleRepository.save(entity).getBlueRuleId();
            if (id > 0)
                count++;
        }
        return count;
    }

    @Override
    @Transactional
    public Integer deletePortRedRule(String redRuleIds, Integer ruleModelId) throws Exception {
        String[] list = redRuleIds.split(",");
        for (String redRuleId : list) {
            portRedRuleRepository.delete(Integer.parseInt(redRuleId));
        }
        Long GreenTime = new Date().getTime() / 1000;
        String strGreenTime = GreenTime.toString();//TODO 更新表即执行版本更新
        portRedRuleRepository.updateRuleMark(strGreenTime, ruleModelId);
        return list.length;
    }

    @Override
    public Integer deletePortBlueRule(String blueRuleIds, Integer alarmId) throws Exception {
        String[] list = blueRuleIds.split(",");
        for (String redRuleId : list) {
            portBlueRuleRepository.delete(Integer.parseInt(redRuleId));
        }
        return list.length;
    }

    @Override
    @Transactional
    public Integer addRedRule(PortRedRuleDto redRuleDto) {
        return portRedRuleRepository.save(redRuleDto.toBean()).getRedRuleId();
    }

    @Override
    @Transactional
    public Integer addBlueRule(PortBlueRuleDto blueRuleDto) throws Exception {
        return portBlueRuleRepository.save(blueRuleDto.toBean()).getBlueRuleId();
    }

}
